<template>
  <Container class="rank-list-setting">
    <div class="container-header" slot="header">
      <headForm :list="list" @onSearch="onSearch" />
    </div>
    <Tabs v-model="tab">
      <Button size="large" class="item" type="primary" slot="extra" @click="importGoal()">经销商目标数导入</Button>
      <Button size="large" class="item" type="primary" slot="extra" @click="preserve">系数维护</Button>
      <Button size="large" class="item" type="primary" slot="extra" @click="rankOfficeSet">排行榜科室设置</Button>
      <Button size="large" class="item" type="primary" slot="extra" @click="rankSet">排行榜标签设置</Button>
      <export-btn @onExport="onExport" class="item" slot="extra"></export-btn>
      <TabPane label="排行榜设置" name="tab1">
        <Table
          :height="options.tableHeight"
          stripe
          border
          :columns="list.columns"
          :data="list.data"
          no-data-text="亲，没有找到相关记录哦！~"
        ></Table>
        <div class="pager">
          <Page
            :total="list.totalItem"
            :current="list.currentPage"
            :page-size="list.pageSize"
            @on-change="onPageChange"
            @on-page-size-change="onPageSizeChange"
            show-sizer
            placement="top"
          ></Page>
        </div>
        <Loader :status="list.status" @refresh="getData"></Loader>
      </TabPane>
    </Tabs>
    <!-- <NumItem ref="numEdit" @complete="getData"></NumItem> -->
    <importsEditItem ref="importsEditItem" @complete="getData"></importsEditItem>
    <preserve ref="preservePage" @complete="getData"></preserve>
    <rankOfficeSet ref="rankOfficeSet" @complete="getData"></rankOfficeSet>
    <rankSet ref="rankSet" @complete="getData"></rankSet>
    <onEdit ref="onEdits" @complete="getData"></onEdit>
  </Container>
</template>
<script>
import table from "@/table";
import rankList from "./list/rankList";
import importsEditItem from "./edit/importsEditItem";
import preserve from "./edit/preserveEditItem";
import rankOfficeSet from "./edit/rankOfficeSetEditItem";
import rankSet from "./edit/rankSetEditItem";
import onEdit from "./edit/editItem";
import headForm from "./list/headForm";

import { clearEmpty, formatDate, formatMonth, getOriginData } from "@/util";
// import NumItem from "./edit/NumItem";
export default {
  name: 'setting/rank/list',
  components: {
    importsEditItem,
    preserve,
    rankSet,
    rankOfficeSet,
    onEdit,
    headForm
  },
  data() {
    return {
      tab: "tab1",
      list: rankList,
      options: {
        tableHeight: 300
      }
    };
  },
  mounted() {
    this.initTable();
    this.$bus.$on("onEdit", this.onEdit);
    this.getData();
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    ...table,
    onSearch() {
      this.list.currentPage = 1;
      this.getData();
    },
    getData() {
      let v = this.dealTime();
      if (!v) return;
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        ...this.dealTime()
      };
      param = clearEmpty(param);

      this.list.status = "loading";
      this.$ajaxGet("/report/rank/page", param)
        .then(data => {
          this.list.data = [];
          this.list.totalItem = Number(data.total);
          this.list.data = data.records || [];
          this.list.status = "pass";
          this.backTableTop();
        })
        .catch(res => {
          if (!res.success && res.code === 100050) {
            this.list.status = "pass";
          } else {
            this.list.status = "error";
          }
        });
    },
    importGoal() {
      this.$refs.importsEditItem.openModal();
    },
    preserve() {
      this.$refs.preservePage.openModal();
    },
    rankSet() {
      this.$refs.rankSet.openModal();
    },
    rankOfficeSet() {
      this.$refs.rankOfficeSet.openModal();
    },
    onEdit(row) {
      this.$refs.onEdits.updateForm(row);
    },
    onExport() {
      let v = this.dealTime();
      if (!v) return;
      let param = {
        ...this.dealTime()
      };
      param = clearEmpty(param);
      this.$jsonPost("/excel/export/exportRank", param).then(res => {
          this.list.status = "pass";
          this.$Modal.success({
            title: "导出成功",
            content: "请到首页-导出列表处下载",
            onOk: () => {
              this.$router.push('/home/export/list')
            }
          });
        })
        .catch(err => {
          this.list.status = "pass";
        });
    },
    onPageChange(number) {
      this.list.currentPage = number;
      this.getData();
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1;
      this.list.pageSize = pageSize;
      this.getData();
    },
    onChangeStartMonth(v) {
      this.list.form.beginYearmonth = v;
      if (!this.list.form.endYearmonth) {
        this.list.form.endYearmonth = v;
      } else {
        this.timeSelectDeal("beginYearmonth");
      }
    },
    onChangeEndMonth(v) {
      this.list.form.endYearmonth = v;
      if (!this.list.form.beginYearmonth) {
        this.list.form.beginYearmonth = v;
      } else {
        this.timeSelectDeal("endYearmonth");
      }
    },
    dealTime() {
      let params = { ...this.list.form };
      if (!params.beginYearmonth) {
        this.$Message.info("请选择开始时间");
        return null;
      }
      if (!params.endYearmonth) {
        this.$Message.info("请选择结束时间");
        return null;
      }
      if (params.beginYearmonth && !params.endYearmonth) {
        this.$Message.info("请选择结束时间");
        return null;
      }
      if (!params.beginYearmonth && params.endYearmonth) {
        this.$Message.info("请选择开始时间");
        return null;
      }
      if (params.beginYearmonth && params.endYearmonth) {
        params.beginYearmonth = formatMonth(params.beginYearmonth).replace(
          "-",
          ""
        );
        params.endYearmonth = formatMonth(params.endYearmonth).replace("-", "");
      }
      return params;
    }
  },
  watch: {
  }
};
</script>